/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ui;

import controller.PessoaDAO;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import models.Pessoa;
import utils.nucleo.Nucleo;
import utils.observer.Observer;

/**
 *
 * @author Amauri
 */
public class GridPessoas extends javax.swing.JPanel {

    private final class GridPessoasObserver implements Observer {
        @Override
        public void update() {
            loadGrid();
        }
    }

    public GridPessoas() {
        initComponents();
        
        PessoaDAO.getInstance().getSubject().registerObserver(new GridPessoasObserver());
       
        loadGrid();
    }
    
    public Pessoa getPessoaSelecionadaGrid(){
        
        if( jTListaPessoas.getSelectedRow() == -1 ){
            JOptionPane.showMessageDialog(null, "Selecione uma pessoa!");
        }
        
        return (Pessoa)jTListaPessoas.getModel().getValueAt(jTListaPessoas.getSelectedRow(), jTListaPessoas.getColumnCount()-1);
    }
    
    protected void loadGrid(){
        DefaultTableModel modelo = (DefaultTableModel) jTListaPessoas.getModel();
        
        modelo.setRowCount(0);
        
        for (Pessoa p: PessoaDAO.getPessoas()) {
            modelo.addRow(new Object[]{
                p.getId(),
                p.getNome(),
                p.getCpf(),
                p
            });
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jBNovaPessoa = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTListaPessoas = new javax.swing.JTable();
        jBExcluir = new javax.swing.JButton();
        jBAlterarPessoa = new javax.swing.JButton();

        jButton1.setText("jButton1");

        jBNovaPessoa.setText("Nova Pessoa");
        jBNovaPessoa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBNovaPessoaActionPerformed(evt);
            }
        });

        jLabel1.setText("LISTA DE PESSOAS");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(152, 152, 152)
                .addComponent(jLabel1)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jTListaPessoas.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null}
            },
            new String [] {
                "Código", "Nome", "CPF", "null"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.Object.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTListaPessoas.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane2.setViewportView(jTListaPessoas);
        jTListaPessoas.getColumnModel().getColumn(3).setMinWidth(0);
        jTListaPessoas.getColumnModel().getColumn(3).setPreferredWidth(0);
        jTListaPessoas.getColumnModel().getColumn(3).setMaxWidth(0);

        jBExcluir.setText("Excluir");
        jBExcluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBExcluirActionPerformed(evt);
            }
        });

        jBAlterarPessoa.setText("Alterar Pessoa");
        jBAlterarPessoa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBAlterarPessoaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 421, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jBNovaPessoa)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jBAlterarPessoa)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jBExcluir)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 225, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jBNovaPessoa)
                    .addComponent(jBExcluir)
                    .addComponent(jBAlterarPessoa)))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jBNovaPessoaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBNovaPessoaActionPerformed
        Nucleo.getInstance().getPrincipal().abrirTela(CadPessoa.class, false);
    }//GEN-LAST:event_jBNovaPessoaActionPerformed

    private void jBExcluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBExcluirActionPerformed
        PessoaDAO.excluir(this.getPessoaSelecionadaGrid());
    }//GEN-LAST:event_jBExcluirActionPerformed

    private void jBAlterarPessoaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBAlterarPessoaActionPerformed
        Nucleo.getInstance().getPrincipal().abrirTela(
            new CadPessoa(this.getPessoaSelecionadaGrid()),
            false
        );
    }//GEN-LAST:event_jBAlterarPessoaActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBAlterarPessoa;
    private javax.swing.JButton jBExcluir;
    private javax.swing.JButton jBNovaPessoa;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTListaPessoas;
    // End of variables declaration//GEN-END:variables
}
